﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace AllegroWebAPI.Utils
{

    public class ApiUtils
    {
        private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public static long B2L(bool v)
        {
            return v ? 1 : 0;
        }

        public static long D2L(DateTime val)
        {
            return Convert.ToInt64(val.Subtract(new DateTime(1899, 12, 30, 0, 0, 0, DateTimeKind.Utc)).TotalDays);
        }
        public static long D2Unix(DateTime val)
        {
            return (long)(val - UnixEpoch).TotalSeconds;
        }

        public static DateTime Unix2D(long val)
        {
            //return UnixEpoch.AddMilliseconds(val);
            return UnixEpoch.AddSeconds(val);
        }
    }

    public class AllegroFormating
    {
        CultureInfo ciDot;

        static char[] whitespaceAndZero = new[] {
            ' ',
            '\t',
            '\r',
            '\n',
            '\u000b', // vertical tab
            '\u000c', // form feed
            '0'
        };

        static string TrimEndWhitespaceAndZeros(string s)
        {
            return s.Contains('.') ? s.TrimEnd(whitespaceAndZero) : s;
        }

        static bool TryParseAfterTrim(string s, out decimal d)
        {
            return Decimal.TryParse(TrimEndWhitespaceAndZeros(s), out d);
        }

        public AllegroFormating()
        {
            ciDot = CultureInfo.InvariantCulture.Clone() as CultureInfo;
            ciDot.NumberFormat.NumberDecimalSeparator = ".";
        }

        public int ToInt(string s)
        {
            int result;
            if (!int.TryParse(TrimEndWhitespaceAndZeros(s), out result))
                result = 0;
            return result;
        }

        public decimal ToDecimal(string s)
        {
            decimal result;
            if (!decimal.TryParse(TrimEndWhitespaceAndZeros(s), NumberStyles.Any, ciDot, out result))
                result = 0;

            return result;
        }

        public DateTime ToDate(string s)
        {
            DateTime result;
            if (!DateTime.TryParse(TrimEndWhitespaceAndZeros(s), out result))
                result = DateTime.MinValue;

            return result;
        }

    }

    /*
    public class AuctionParameters
    {
        FieldsValueList vl = new FieldsValueList();

        public ParameterList LoadFromFile(int CategoryId, string fileName)
        {
            ParameterList plist = new ParameterList();
            ParameterList ret = new ParameterList();
            plist.LoadFromFile(fileName);

            foreach (ParameterItem itm in plist)
            {
                if (itm.CategoryId == 0 || itm.CategoryId == CategoryId)
                {
                    ret.Add(itm);

                    //vl.Set(itm.Id, itm.);
                }

            }
            return ret;
        }
        public bool LoadFromServer(int CategoryId)
        {
            return true;
        }
    }
    
    public class AuctionValueList : FieldsValueList
    {
        public int GetInt(NAFid fid)
        {
            return items[(int)fid].fvalueInt;
        }
        public float GetFloat(NAFid fid)
        {
            return items[(int)fid].fvalueFloat;
        }
        public string GetString(NAFid fid)
        {
            return items[(int)fid].fvalueString;
        }
        public DateTime GetDateTime(NAFid fid)
        {
            return DateTime.Now; // items[(int)fid].fvaluedatetime;
        }


        public FieldsValueList AddInt(NAFid fid, int val)
        {

            return Set((int)fid, val);
        }

        public FieldsValueList AddFloat(NAFid fid, float val)
        {
            return Set((int)fid, val);
        }

        public FieldsValueList AddDateTime(NAFid fid, DateTime val)
        {
            return Set((int)fid, val);
        }

        public FieldsValueList AddString(NAFid fid, string val)
        {
            return Set((int)fid, val);
        }
    }

*/

}
